Reverse Integer
Question
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Analysis
越界判断:
tmp加上tail后看是否还能变回原来的数字,不可直接利用int型的加和来判断越界(tmp*10+tail>Integer.MAX_VALUE)
Code
|
|
Reverse Bit
Question
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
Analysis
- 每次取出数字n的最后一位给res,res在前31位的时候不断左移
- 优化后,将32-bit分为四段,利用map来保存倒转的结果
Code
Basic Version
|
|
Optimized Version
|
|